package com.kancy.vip.pattern.factory.simplefactory;
import com.kancy.vip.pattern.factory.Phone;
import com.kancy.vip.pattern.factory.products.HuaWeiPhone;
import com.kancy.vip.pattern.factory.products.SanXingPhone;

public class SimplePhoneFactory {

    public static Phone createPhone(String brand){
        switch (brand){
            case "huawei":return new HuaWeiPhone();
            case "sanxing":return new SanXingPhone();
            default:
                try {
                    return createPhone(Class.forName(brand));
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
        }
        return null;
    }

    public static Phone createPhone(Class clazz){
        try {
            if(clazz == null){
                return null;
            }
            return (Phone) clazz.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

}
